package org.mparts.client.activity;

import java.util.List;

import org.mparts.client.content.Strings;
import org.mparts.client.ui.ILoadCatalogView;
import org.mparts.client.ui.ILoadCatalogView.ILoadCatalogPresenter;
import org.mparts.client.ui.editor.IPartitemEditorView;
import org.mparts.client.ui.editor.IPartitemEditorView.IPartitemEditorPresenter;
import org.mparts.client.ui.editor.PartitemEditor.Driver;
import org.mparts.client.ui.events.LoadCatalogEvent;
import org.mparts.client.ui.events.LoadCatalogEventHandler;
import org.mparts.client.ui.events.ReloadPartitemListEvent;
import org.mparts.client.ui.events.SaveEvent;
import org.mparts.client.ui.events.SaveEventHandler;
import org.mparts.client.ui.events.ShowPartitemEvent;
import org.mparts.client.utils.Utils;
import org.mparts.shared.MpartsRequestFactory;
import org.mparts.shared.proxy.AutoProxy;
import org.mparts.shared.proxy.ModelProxy;
import org.mparts.shared.proxy.PartgroupProxy;
import org.mparts.shared.proxy.PartitemProxy;
import org.mparts.shared.request.MpartsRequest;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.RequestContext;
import com.sencha.gxt.widget.core.client.box.AlertMessageBox;
import com.sencha.gxt.widget.core.client.box.AutoProgressMessageBox;
import com.sencha.gxt.widget.core.client.box.MessageBox;
import com.sencha.gxt.widget.core.client.event.SubmitCompleteEvent;
import com.sencha.gxt.widget.core.client.event.SubmitCompleteEvent.SubmitCompleteHandler;

public class LoadCatalogActivity extends AbstractMpartsActivity implements ILoadCatalogPresenter {
	
	private ILoadCatalogView display;
	private EventBus eventBus;
	
	@Inject
	Strings strings;

//	private AutoProgressMessageBox progressBox;

	@Inject
	public LoadCatalogActivity(MpartsRequestFactory rf, EventBus eventBus, ILoadCatalogView display) {
		this.display = display;
		this.rf = rf;
		this.eventBus = eventBus;
		if ( display != null ) {			
			bind();
		}
	}
	
	private void bind() {
		display.setPresenter(this);
//		driver = display.getDriver();
		eventBus.addHandler(LoadCatalogEvent.TYPE, new LoadCatalogEventHandler() {
			
			@Override
			public void onLoad(LoadCatalogEvent event) {
				Log.debug("load catalog event process...");
				display.getDialog().show();
			}
		});	
		display.getForm().addSubmitCompleteHandler(new SubmitCompleteHandler() {
			
			@Override
			public void onSubmitComplete(SubmitCompleteEvent event) {
				Log.debug("upload complete");
				Utils.getMpartsService().getLoadCatalogResult(new AsyncCallback<List<String>>() {
					
					@Override
					public void onSuccess(List<String> result) {
						display.showProgress(false);
						display.getDialog().hide();
						AlertMessageBox d;
						if ( result != null && result.size() > 0 ) {
							d = new AlertMessageBox(Utils.getStrings().alertTitle(), result.get(0));
						} else {
							d = new AlertMessageBox(Utils.getStrings().alertTitle(), Utils.getStrings().loadCatalogSuccess());
						}
						d.show();
					}
					
					@Override
					public void onFailure(Throwable caught) {
						display.showProgress(false);
						display.getDialog().hide();
						AlertMessageBox d = new AlertMessageBox(Utils.getStrings().alertTitle(), caught.getLocalizedMessage());
						d.show();
					}
				});
//				if ( progressBox != null && progressBox.isVisible() ) {
//					display.getDialog().hide();
//					progressBox.hide();
//				}
				
			}
		});
	}

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		display.getDialog().show();
	}

	@Override
	public void goTo(Place place) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void process() {
		Utils.getMpartsService().getUploadUrl("/mparts/upload?store=" + Utils.getCurrentStoreId(),
				new AsyncCallback<String>() {


					@Override
					public void onSuccess(String result) {
						Log.debug("upload URL: " + result);
						display.getForm().setAction(result);
						display.getForm().submit();
						display.showProgress(true);
//						display.getForm().setVisible(false);
////						display.getDialog().hide();
//						if ( progressBox == null ) {
//							progressBox = new AutoProgressMessageBox(strings.waitTitle(), strings.waitDesc());
//							progressBox.auto();
//						}
//						progressBox.show();
					}

					@Override
					public void onFailure(Throwable caught) {
						Log.error("get upload URL failed", caught);
					}
				});
		
	}



}
